package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.ZyRepair;
import com.blb.zycommunity.entity.vo.RepairQuery;
import com.blb.zycommunity.entity.vo.ZyOwenVo;
import com.blb.zycommunity.entity.vo.ZyRepairVo;
import com.blb.zycommunity.mapper.ZyRepairMapper;
import com.blb.zycommunity.service.IZyRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 报修信息 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RequestMapping("uni-app")
@RestController
@CrossOrigin
public class ZyRepairController {

    @Autowired
    private IZyRepairService iZyRepairService;

    @Resource
    private ZyRepairMapper zyRepairMapper;

    /**
     * 查询全部报修记录并分页
     */
    @PostMapping("/repair-page")
    public ResponseResult findAllRepair(@RequestBody RepairQuery repairQuery){
        Page<ZyRepair> zyRepairPage = new Page<>(repairQuery.getPageNum(), repairQuery.getPageSize());
        Page<ZyRepair> repairPage = zyRepairMapper.findRepairPage(zyRepairPage, repairQuery.getRepairScope(),
                repairQuery.getRepairContent(), repairQuery.getStartTime(), repairQuery.getFinishTime());

        return ResponseResult.ok(repairPage);
    }

    /**
     * 根据业主名查询全部保修记录
     */
    @PostMapping("/repair-all")
    public ResponseResult findAll(@RequestBody ZyRepairVo zyRepairVo){
        List<ZyRepair> repairs = zyRepairMapper.findRepair(zyRepairVo.getUserName(),zyRepairVo.getKeyword(),zyRepairVo.getCurNow());
        return ResponseResult.ok(repairs);
    }

    /**
     * 根据业主名查询房屋
     */
    @GetMapping("/repair-name")
    public ResponseResult findName(@RequestParam String name){
        List<ZyOwenVo> byName = zyRepairMapper.findByName(name);
        return ResponseResult.ok(byName);
    }

    /**
     * 更改报修单状态
     */
    @PutMapping("/repair-status")
    public ResponseResult updateStatus(@RequestBody ZyRepair zyRepair){
        iZyRepairService.updateById(zyRepair);
        return ResponseResult.ok("ok");
    }


    /**
     * 添加报修
     */
    @PostMapping("/repair-add")
    public ResponseResult findAdd(@RequestBody ZyRepair zyRepair){
        // 生成 UUID
        UUID uuid = UUID.randomUUID();
        zyRepair.setRepairNum(uuid.toString().substring(0, 8));
        zyRepair.setCreateTime(LocalDateTime.now());
        iZyRepairService.save(zyRepair);
        return ResponseResult.ok("ok");
    }

    /**
     * 删除报修记录del_flag为1
     */
    @DeleteMapping("/repair-delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        zyRepairMapper.updateDelFlag(id);
        return ResponseResult.ok("ok");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/repair-deleteLog/{ids}")
    public ResponseResult deleteById(@PathVariable Long[] ids){
        for (Long id : ids) {
            iZyRepairService.removeById(id);
        }
        return ResponseResult.ok("删除成功");
    }



}
